package com.fc.remoting.message;

import com.alibaba.fastjson.JSONObject;
import com.fc.commons.JsonUtils;
import com.fc.commons.security.MD5Util;

public class DefaultRemotingMessage {
	
	public static RemotingMessage heartBeatMessage(){	
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.HEART_BEAT);
		RemotingMessage message = RemotingMessage.createMessage(header, new byte[]{});
		return message;
	}
	
	public static RemotingMessage connectMessage(String appkey,String clientId,String deviceId){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.CONNECT);
		JSONObject body = new JSONObject();
		body.put("module", 1);
		body.put("order", 1);
		body.put("appkey", appkey);
		body.put("me_client_id", clientId);
		body.put("device_id", deviceId);
		body.put("platform", "Android");
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, body.toJSONString().getBytes());
		return remotingMessage;
	}
	
	public static RemotingMessage connectAckMessage(){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.CONNECT_ACK);
		RemotingMessage message = RemotingMessage.createMessage(header, new byte[]{});
		return message;
	}
	
	public static RemotingMessage sendMessage(String clientId,Object message){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.MESSAGE);
		JSONObject body = new JSONObject();
		body.put("module", 1);
		body.put("order", 5);
		body.put("msg_content", message);
		body.put("from_client_id", clientId);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, JsonUtils.object2Bytes(body));
		return remotingMessage;
	}
	
	public static RemotingMessage closeMessage(String appkey,String clientId){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.LOGOUT);
		JSONObject body = new JSONObject();
		body.put("module", 1);
		body.put("order", 6);
		body.put("client_id", clientId);
		body.put("appkey",appkey);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, body.toJSONString().getBytes());
		return remotingMessage;
	}
	
	public static RemotingMessage kickMessage(){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.KICK);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, new byte[]{});
		return remotingMessage;
	}
	
	public static RemotingMessage registerMessage(String signkey,int imPort){
		JSONObject body = new JSONObject();
		long timeStamp = System.currentTimeMillis();
		body.put("module", 1);
		body.put("order", 1);
		body.put("time_stamp", System.currentTimeMillis());
		String token = MD5Util.Instance.getInstance().md5(timeStamp + signkey);
		body.put("token", token);
		body.put("im_port", imPort);
		
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.MESSAGE);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, JsonUtils.object2Bytes(body));
		return remotingMessage;
	}
	
	public static RemotingMessage unRegisterMessage(String signkey,int imPort){
		JSONObject body = new JSONObject();
		long timeStamp = System.currentTimeMillis();
		body.put("module", 1);
		body.put("order", 6);
		body.put("time_stamp", System.currentTimeMillis());
		String token = MD5Util.Instance.getInstance().md5(timeStamp + signkey);
		body.put("token", token);
		body.put("im_port", imPort);
		
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.MESSAGE);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, JsonUtils.object2Bytes(body));
		return remotingMessage;
	}
	
	public static RemotingMessage updateChannelMessage(){
		RemotingHeader header = new RemotingHeader();
		header.setMsgType(RemotingMessageType.UPDATE_CHANNEL);
		RemotingMessage remotingMessage = RemotingMessage.createMessage(header, new byte[]{});
		return remotingMessage;
	}
}
